import { get } from '@/utils/http'

// 获取轮播图数据
export const getSwipersDataApi = () => get(`/api/cookbook/swipers`)

// 获取热门分类数据
export const hotCategoryApi = () => get(`/api/cookbook/hotcate`)

// 精品菜谱
export const goodFoodApi = (page = 1) => get(`/api/cookbook/goodfood?page=${page}`)

// 根据菜谱id返回对应的详情信息
export const getCookbookIdToInfoApi = async id => {
  let ret = await get(`/api/cookbook/detail/${id}`)
  if (ret.code === 0) {
    // 正则匹配img标签
    let reg = /<img.*?>/g
    ret.data.content = ret.data.content.replace(reg, match => {
      // 查找一下当前img它有不有width属性,如果有就不加,没有就加上 100%
      if (!match.includes('width')) {
        match = match.replace(/[\/]?>/, ` width="100%" />`)
      }
      return match
    })

    return ret.data
  } else {
    return null
  }
}
